<template>
	<view>
		<view class="main">
			<u-form :model="form" ref="uForm" label-width="160">
				<u-form-item label="头像">
					<u-avatar :src="src" size="120"></u-avatar>
				</u-form-item>
				<u-form-item label="昵称">
					<u-input v-model="form.nickName"></u-input>
				</u-form-item>
				<u-form-item label="性别">
					<u-radio-group v-model="radio" @change="change">
									<u-radio v-for="(item, index) in radioList" :key="index" :name="item.name" :disabled="item.disabled">
										{{ item.name }}
									</u-radio>
								</u-radio-group>
				</u-form-item>
				<u-form-item label="密码">
					<u-input v-model="form.password"></u-input>
				</u-form-item>
				<u-form-item label="确认密码" prop="repassword">
					<u-input v-model="form.repassword"></u-input>
				</u-form-item>
				<u-form-item label="电话号码">
					<u-input v-model="form.phonenumber"></u-input>
				</u-form-item>
				<u-form-item label="邮箱">
					<u-input v-model="form.email"></u-input>
				</u-form-item>
				<u-form-item label="身份证">
					<u-input v-model="form.idCard"></u-input>
				</u-form-item>
			</u-form>
		</view>
		<button @click="submit" class="bu" style="width: 200rpx;height: 80rpx;margin-top: 50rpx;">注册</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				src: '',
				form: {
					avatar: '',
					userName: '',
					nickName: '',
					password: '',
					repassword: '',
					phonenumber: '',
					sex: '',
					email:'',
					idCard:''
				},
				rules: {
					repassword: [
						{
							validator: (rule, value, callback) => {
								if(value === this.form.password){
									callback()
								}else{
									callback(new Error('两次密码输入不一致'))
								}
							},
							trigger: ['blur','change']
						}
					]
				},
				radioList: [
					{
						name: '男',
						required: false
					},
					{
						name: '女',
						required: false
					}
				],
				radio: '男'
			};
		},	
		methods: {
			change(e){
				this.radio = e
				if(this.radio === '男'){
					this.form.sex === '1'
				}else{
					this.form.sex === '0'
				}
			},
			submit(){
				this.$refs.uForm.validate(valid => {
					if(valid){
						this.$request({
							url: '/prod-api/api/register',
							method: 'POST',
							data:{
								...this.form
							}
						}).then(res => {
							if(res.code === 200){
								uni.showToast({
									title:'注册成功'
								})
								setTimeout(() => {
									uni.navigateTo({
										url:'/pages/login/login'
									})
								}, 1000)
							}else{
								uni.showToast({
									icon:'error',
									title:res.msg
								})
							}	
						})
					} 
				})
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		}
	}
</script>

<style lang="scss">
view{
	padding: 20rpx;
	.main{
		padding: 20rpx;
	}
}
</style>
